using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Pokemon
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class PanelButton : DrawableGameComponent, ITappedInner
    {

        #region Attribute

        private Texture2D _background;

        private Vector2 _pos;

        private Vector2 _size;        

        private List<Grid> _grid = new List<Grid>();

        private readonly GameScreen _owner;

        private string _path;

#endregion

        public PanelButton(Game game, GameScreen owner, string path)
            : base(game)
        {
            _owner = owner;

            _path = path;    
        
            Initialize();
        }

        #region Properties

        public GameScreen Owner
        {
            get { return _owner; }
        }

        public List<Grid> Grid
        {
            get { return _grid; }
            set { _grid = value; }
        }

        #endregion

        #region Initialize

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            if (_path != null)
            {                
                XDocument doc = XDocument.Load(_path);
                if (doc.Document != null)
                {
                    XName name = XName.Get(@"Source");
                    
                    var definitions = doc.Document.Descendants(name);
                    var sourcePath = (definitions.First()).Value;

                    if (sourcePath != "")
                        _background = Owner.CurrGame.Content.Load<Texture2D>(sourcePath);
                    name = XName.Get(@"Grid");
                    definitions = doc.Document.Descendants(name);
                    foreach (var definition in definitions)
                    {
                        sourcePath = definition.Value;
                        Grid g = new Grid(Game, Owner, sourcePath);
                        Grid.Add(g);
                        g.ShowGridLine();
                        
                    }                                       
                    
                    name = XName.Get(@"Pos");
                    definitions = doc.Document.Descendants(name);
                    var xAttribute = definitions.First().Attribute(@"X");
                    if (xAttribute != null)
                    {
                        var attribute = definitions.First().Attribute(@"Y");
                        if (attribute != null)
                            _pos = new Vector2(Int32.Parse(xAttribute.Value),
                                                 Int32.Parse(attribute.Value));
                    }
                    name = XName.Get(@"Size");
                    definitions = doc.Document.Descendants(name);
                    xAttribute = definitions.First().Attribute(@"Width");
                    if (xAttribute != null)
                    {
                        var attribute = definitions.First().Attribute(@"Height");
                        if (attribute != null)
                            _size = new Vector2(Int32.Parse(xAttribute.Value),
                                                 Int32.Parse(attribute.Value));
                    }    
                    
                    
                }                              
            }
            base.Initialize();
        }

#endregion

        #region Draw & Update

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            foreach (var grid in Grid)
            {
                grid.Update(gameTime);
            }
            //_grid.Update(gameTime);
            base.Update(gameTime);
        }

        /// <summary>
        /// Draw components on the screen
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            if (_background != null)
                Owner.ScreenManager.SpriteBatch.Draw(_background, 
                    new Rectangle((int)_pos.X, (int)_pos.Y, (int)_size.X, (int)_size.Y), Color.White);
            foreach (var grid in Grid)
            {
                grid.Draw(gameTime);
            }
            //_grid.Draw(gameTime);
            base.Draw(gameTime);
        }

#endregion

        #region Support Method

        /// <summary>
        /// ITappedInner method
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool CheckTapped(Point p)
        {
            Rectangle rect = new Rectangle((int)_pos.X, (int)_pos.Y, (int)_size.X, (int)_size.Y);
            bool isContain = rect.Contains(p);
            if (isContain)
            {
                foreach (var grid in Grid)
                {
                    grid.CheckTapped(p);                    
                }                
            }
            return isContain;
        }

        /// <summary>
        /// Lay ra PushBlank ma user da select de tao ra Pokemon tuong ung
        /// </summary>
        /// <returns></returns>
        public PushalbeBlank GetPushBlank()
        {            
            foreach (var grid in Grid)
            {
                if (grid.SelectedBlank != null)
                    return grid.SelectedBlank;
            }
            return null;
            //return _grid.SelectedBlank;
        }

        public void GridPushed()
        {
            foreach (var grid in Grid)
            {
                grid.OnPush();
            }            
        }

        public bool InsertCard(Card c)
        {
            foreach (var grid in _grid)
            {
                if (grid.InsertCard(c))
                {
                    return true;
                }                
            }
            return false;
        }
#endregion
    }
}
